package com.designpattern.observer;

import java.util.ArrayList;

/**
 * @author liyanhao
 * @Date 2023-02-17 10:10
 */
public class WeatherData implements Subject {

    //观察者数据 存放所有观察者
    private ArrayList<Observer> observers;
    //温度
    private float temperature;
    //湿度
    private float humidity;
    //压力
    private float presure;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

    //注册观察者
    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    //移除观察者
    @Override
    public void removeObservre(Observer o) {
        observers.remove(o);
    }

    //通知观察者
    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update(this.temperature,this.humidity,this.presure);
        }
    }
    //信息更新
    public void measurementsChanged(){
        notifyObserver();
    }

    //设置天气
    public void setMeaSureMents(float temperature, float humidity, float presure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.presure = presure;
        measurementsChanged();//设置完成天气就已经改变了
    }
}
